package com.xhdd.controller;


import com.xhdd.dto.*;
import com.xhdd.serv.CodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.xhdd.constant.Const.k;

@RestController
@RequestMapping("code")
@Api(tags = "编码服务")
public class CodeController {
    @Resource
    CodeService codeService;

    //    @Resource
//    KafkaTemplate<String, Object> kTemplate;
//    @Resource
//    AdminClient adminClient;

    @GetMapping("getBaseMsg")
    public Retn getBaseMsg() {
        Map<String, Double> map = new HashMap<>();
        map.put("x", 102.236865);
        map.put("y", 38.548135);
        map.put("xLen", 514.0);
        map.put("yLen", 308.0);
        map.put("cell", 2.0);
        return Retn.ok(map);
    }


    @PostMapping("createIndex")
    @ApiOperation("创建索引")
    public Retn createIndex(@RequestBody MetaData data) throws IOException {
        return Retn.ok(codeService.initMetaTbDoc(data));
    }

    @PostMapping("code")
    @ApiOperation("编码")
    public Retn code(String indexName, String level, @RequestParam("inputStream") MultipartFile file) throws IOException, ParseException, org.locationtech.jts.io.ParseException {
        InputStream inputStream = file.getInputStream();
        CodeData data = new CodeData(indexName, inputStream, level);
        return Retn.ok(codeService.code(data));
    }

    @GetMapping("deleteIndex")
    @ApiOperation("删除索引")
    public Retn deleteIndex(String indexName) throws IOException {
        return Retn.ok(codeService.indexDelete(indexName));
    }

    @GetMapping("listIndex")
    @ApiOperation("获取索引信息列表")
    public Retn listIndex() throws IOException {
        return Retn.ok(codeService.listIndex());
    }


    @PostMapping("searchDoc")
    @ApiOperation("条件检索")
    public Retn searchDoc(@RequestBody ConditionQuery queryBody) throws IOException {
        return Retn.ok(codeService.searchDoc(queryBody.getIndexName(), queryBody.getParams(), queryBody.getPage(), queryBody.getSize()));
    }

    @PostMapping("searchPrefix")
    @ApiOperation("覆盖码检索")
    public Retn searchPrefix(@RequestBody PrefixQuery prefixs) throws IOException {
        return Retn.ok(codeService.searchPrefix(prefixs.getIndexName(), prefixs.getPrefixs(), prefixs.getPage(), prefixs.getSize()));
    }

    @PostMapping("search")
    @ApiOperation("复合检索")
    public Retn searchPrefix(@RequestBody CompositeQuery compositeQuery) throws IOException {
        return Retn.ok(codeService.search(compositeQuery.getIndexName(), compositeQuery.getPrefixs(), compositeQuery.getParams(), compositeQuery.getPage(), compositeQuery.getSize()));
    }

    @GetMapping("propAggs")
    @ApiOperation("求某个属性的最大值、最小值、平均值")
    public Retn propAggs(String indexName, String prop) throws IOException {
        return Retn.ok(codeService.propAggs(indexName, prop));
    }

    @PostMapping("noRepeatCount")
    @ApiOperation("去重统计")
    public Retn noRepeatCount(String indexName, String prop) throws IOException {
        return Retn.ok(codeService.noRepeatCount(indexName, prop));
    }

    @PostMapping("groupBy")
    @ApiOperation("分组统计")
    public Retn groupBy(@RequestBody GroupByQuery groupBy) throws IOException {
        return Retn.ok(codeService.groupBy(groupBy.getIndexName(), groupBy.getParams(), groupBy.getProp(), groupBy.getPropType()));
    }

    @PostMapping("codes2Dj")
    @ApiOperation("网格码转顶角点")
    public Retn codes2Dj(@RequestBody List<String> codes) throws IOException {
        return Retn.ok(codeService.codes2Dj(codes));
    }

    @PostMapping("getStep")
    @ApiOperation("获取网格层级对应的网格长度")
    public Retn getStep(Integer level) throws IOException {
        return Retn.ok(codeService.getStep(level) / k);
    }

    @PostMapping("get4adjoin")
    @ApiOperation("获取网格码的四领域码")
    public Retn get4adjoin(String code) throws IOException {
        return Retn.ok(codeService.get4adjoin(code));
    }


    /*@GetMapping("ktest")
    @ApiOperation("Kafka测试")
    public Retn ktest(String topic, String message) throws IOException {
        kTemplate.send(topic, UUID.randomUUID().toString());
        return Retn.ok("ok");
    }

    @GetMapping("getTopicList")
    @ApiOperation("获取主题列表")
    public Retn getTopicList() throws ExecutionException, InterruptedException {
        ListTopicsResult listTopicsResult = adminClient.listTopics();
        Map<String, TopicListing> map =
                listTopicsResult.namesToListings().get();
        return Retn.ok(map);
    }

    @KafkaListener(groupId = "aa", topics = "kk")
    public void listen(String input) {
        System.out.println("->" + input);
    }*/
}

